﻿
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

// 全局参数定义（实际开发中应通过参数传递）
#define SAMPLE_RATE   44100   // 采样率
#define CARRIER_FREQ  1000    // 载波频率(Hz)
#define MAX_AMPLITUDE 1.0     // 最大振幅
#define PI 3.14159265358979323846

/*------------------------ 基础函数 ------------------------*/

// 生成载波信号（正弦波）
int generate_cover_signal(double* cover, const int size)
{
    if (!cover || size <= 0) return -1;

    for (int i = 0; i < size; ++i)
    {
        double t = (double)i / SAMPLE_RATE;
        cover[i] = MAX_AMPLITUDE * sin(2 * PI * CARRIER_FREQ * t);
    }
    return 0;
}

// 生成数字调制信号（二进制随机序列）
int simulate_digital_modulation_signal(unsigned char* message, const int size) {
    if (!message || size <= 0) return -1;

    for (int i = 0; i < size; ++i)
    {
        message[i] = rand() % 2 + '0'; // 生成0/1序列
    }
    return 0;
}

// 生成模拟调制信号（随机正弦波）
int simulate_analog_modulation_signal(double* message, const int size) {
    if (!message || size <= 0) return -1;

    double mod_freq = 100; // 调制信号频率
    for (int i = 0; i < size; ++i)
    {
        double t = (double)i / SAMPLE_RATE;
        message[i] = 0.5 * sin(2 * PI * mod_freq * t); // 幅度范围[-0.5,0.5]
    }
    return 0;
}

/*------------------------ 数字调制 ------------------------*/

// 数字调频（FSK）
int modulate_digital_frequency(double* cover, const int cover_len,
    const unsigned char* message, const int msg_len) {
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    double freq_shift = 200; // 频偏量
    for (int i = 0; i < cover_len; ++i)
    {
        int symbol_idx = i * msg_len / cover_len; // 符号索引
        double freq = CARRIER_FREQ + (message[symbol_idx] ? freq_shift : -freq_shift);
        double t = (double)i / SAMPLE_RATE;
        cover[i] = MAX_AMPLITUDE * sin(2 * PI * freq * t);
    }
    return 0;
}

// 数字调幅（ASK）
int modulate_digital_amplitude(double* cover, const int cover_len,
    const unsigned char* message, const int msg_len)
{
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    for (int i = 0; i < cover_len; ++i)
    {
        int symbol_idx = i * msg_len / cover_len;
        cover[i] *= (message[symbol_idx] == '1' ? 2.0 : 0.0); // 0/1控制幅度
    }
    return 0;
}

// 数字调相（BPSK）
int modulate_digital_phase(double* cover, const int cover_len,
    const unsigned char* message, const int msg_len)
{
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    for (int i = 0; i < cover_len; ++i)
    {
        int symbol_idx = i * msg_len / cover_len;
        double phase_shift = message[symbol_idx] ? 0 : PI; // 0/π相位
        double t = (double)i / SAMPLE_RATE;
        cover[i] = MAX_AMPLITUDE * sin(2 * PI * CARRIER_FREQ * t + phase_shift);
    }
    return 0;
}

/*------------------------ 模拟调制 ------------------------*/

// 模拟调频（FM）
int modulate_analog_frequency(double* cover, const int cover_len,
    const double* message, const int msg_len)
{
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    double freq_deviation = 500; // 最大频偏
    double phase = 0;
    for (int i = 0; i < cover_len; ++i)
    {
        int msg_idx = i * msg_len / cover_len;
        double freq = CARRIER_FREQ + freq_deviation * message[msg_idx];
        phase += 2 * PI * freq / SAMPLE_RATE;
        cover[i] = MAX_AMPLITUDE * sin(phase);
    }
    return 0;
}

// 模拟调幅（AM）
int modulate_analog_amplitude(double* cover, const int cover_len,
    const double* message, const int msg_len)
{
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    double modulation_index = 0.8; // 调制度
    for (int i = 0; i < cover_len; ++i)
    {
        int msg_idx = i * msg_len / cover_len;
        cover[i] *= (1 + modulation_index * message[msg_idx]);
    }
    return 0;
}

// 模拟调相（PM）
int modulate_analog_phase(double* cover, const int cover_len,
    const double* message, const int msg_len)
{
    if (!cover || !message || cover_len <= 0 || msg_len <= 0) return -1;

    double phase_deviation = PI / 2; // 最大相位偏移
    for (int i = 0; i < cover_len; ++i)
    {
        int msg_idx = i * msg_len / cover_len;
        double t = (double)i / SAMPLE_RATE;
        double phase = 2 * PI * CARRIER_FREQ * t + phase_deviation * message[msg_idx];
        cover[i] = MAX_AMPLITUDE * sin(phase);
    }
    return 0;
}

int main() {
    // 生成参数
    const double duration = 0.001; // 信号时长(秒)
    const int total_samples = SAMPLE_RATE * duration;

    // 内存分配
    double* carrier = (double*)malloc(total_samples * sizeof(double));
    unsigned char* digital_msg = (unsigned char*)malloc(10 * sizeof(unsigned char));
    double* analog_msg = (double*)malloc(total_samples * sizeof(double));
    double* temp = (double*)malloc(total_samples * sizeof(double));

    // 生成信号
    generate_cover_signal(carrier, total_samples);
    simulate_digital_modulation_signal(digital_msg, 10);
    simulate_analog_modulation_signal(analog_msg, total_samples);

    // 输出生成信号
    printf("载波信号：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    printf("数字调制信号：\n");
    for (int i = 0; i < 10; i++)               printf("%c ", digital_msg[i]); puts("");
    printf("模拟调制信号：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", analog_msg[i]); puts("");

    /*------------进行调制------------*/
    // 数字调幅
    memcpy(temp, carrier, sizeof(carrier));
    modulate_digital_amplitude(carrier, total_samples, digital_msg, 10);
    printf("数字调幅：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    memcpy(carrier, temp, sizeof(temp));
    // 数字调相
    memcpy(temp, carrier, sizeof(carrier));
    modulate_digital_phase(carrier, total_samples, digital_msg, 10);
    printf("数字调相：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    memcpy(carrier, temp, sizeof(temp));
    // 模拟调频
    memcpy(temp, carrier, sizeof(carrier));
    modulate_analog_frequency(carrier, total_samples, analog_msg, total_samples);
    printf("模拟调频：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    memcpy(carrier, temp, sizeof(temp));
    // 模拟调幅
    memcpy(temp, carrier, sizeof(carrier));
    modulate_analog_amplitude(carrier, total_samples, analog_msg, total_samples);
    printf("模拟调幅：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    memcpy(carrier, temp, sizeof(temp));
    // 模拟调相
    memcpy(temp, carrier, sizeof(carrier));
    modulate_analog_phase(carrier, total_samples, analog_msg, total_samples);
    printf("模拟调相：\n");
    for (int i = 0; i < total_samples; i++)    printf("%lf ", carrier[i]);    puts("");
    memcpy(carrier, temp, sizeof(temp));



    // 释放内存
    free(carrier);
    free(digital_msg);
    free(analog_msg);
    free(temp);
    return 0;
}